<template>
  <div>
    <el-card class="box-card">
      <p>
        <el-button type="primary" @click="dialogVisible = true"
          >添加角色</el-button
        >
      </p>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column type="expand">
          <template v-slot="scope">
           
           
     <el-row  v-for="(item1) in scope.row.children" :key="item1.id">
            <!-- 渲染一级权限 -->
            <el-col :span="5">
                <el-tag>
                    {{item1.authName}}
                </el-tag>
                <i class="el-icon-caret-right"></i>
            </el-col>
            <!-- 渲染二，三级权限 -->
            <el-col :span="19">
                <!-- 通过for循环嵌套渲染二级权限  -->
                <el-row  v-for="(item2) in item1.children" :key="item2.id">
                    <el-col :span="6">
                        <el-tag type="success">{{item2.authName}}</el-tag>
                        <i class="el-icon-caret-right"></i>
                    </el-col>
                    <el-col :span="18">
                        <el-tag type="warning" v-for="(item3) in item2.children" :key="item3.id">
                            {{item3.authName}}
                        </el-tag>
                    </el-col>
                </el-row>
            </el-col>
        </el-row>


          </template>
        </el-table-column>
        <el-table-column label="#" type="index"> </el-table-column>

        <el-table-column label="角色" prop="roleName"> </el-table-column>
        <el-table-column label="角色描述" prop="roleDesc"> </el-table-column>
        <el-table-column label="操作">
          <template v-slot="scope">
            <el-button
              type="warning"
              icon="el-icon-edit"
              @click="f2(scope.row)"
            ></el-button>
            <el-button type="primary" icon="el-icon-share"></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              @click="f1(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <div>
      <!-- 添加角色 -->
      <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
        <span>
          <el-form>
            <el-form-item label="角色">
              <el-input v-model="formLabelAlign.roleName"></el-input>
            </el-form-item>
            <el-form-item label="角色描述">
              <el-input v-model="formLabelAlign.roleDesc"></el-input>
            </el-form-item>
          </el-form>
        </span>

        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="tj()">确 定</el-button>
        </span>
      </el-dialog>
    </div>
    <div>
      <!-- 修改角色 -->
      <el-dialog title="提示" :visible.sync="fa" width="30%">
        <span>
          <el-form>
            <el-form-item label="角色">
              <el-input v-model="xg.roleName"></el-input>
            </el-form-item>
            <el-form-item label="角色描述">
              <el-input v-model="xg.roleDesc"></el-input>
            </el-form-item>
          </el-form>
        </span>

        <span slot="footer" class="dialog-footer">
          <el-button @click="fa = false">取 消</el-button>
          <el-button type="primary" @click="f3()">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      dialogVisible: false,
      tableData: [],
      formLabelAlign: {
        roleName: "",
        roleDesc: "",
      },
      xg: {
        roleName: "",
        roleDesc: "",
      },
      fa: false,
    };
  },
  created() {
    this.googet();
  },
  methods: {
    async f3() {
  await this.axios.put(`roles/${this.xg.id}`,this.xg)
      this.googet();
      this.fa=false
    },
    f2(ev) {
      this.fa = true;
      this.xg = ev;
    },
    async f1(ev) {
      await this.axios.delete(`/roles/${ev.id}`);

      this.googet();
    },
    async tj() {
      let { data: res } = await this.axios.post("/roles", this.formLabelAlign);
      console.log(res);
      this.googet();
      this.dialogVisible = false;
    },
    async googet() {
      let res = await this.axios.get("/roles");
      console.log(res.data.data);
      this.tableData = res.data.data;
    },
  },
};
</script>